const fs = require('fs')
const { join } = require('path')

async function writeTemplate(name, path){
  const dirPath = path 
    ? join(__dirname, '..', 'src', path, name)
    : join(__dirname, '..', 'src', name)
  
  if(!fs.existsSync(dirPath)){
    fs.mkdirSync(dirPath)
  }

  const template = `import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
import { IsNotEmpty, IsString } from "class-validator";

@Entity('${name}')
export class ${name.charAt(0).toUpperCase() + name.slice(1)} {
  @PrimaryGeneratedColumn()
  id: number
}
`
  const filePath = join(dirPath, `${name}.entity.ts`)
  fs.writeFileSync(filePath, template)
  console.log(`成功：创建文件 ${filePath}`)
}

async function start(){
  const name = process.argv[2]
  if(!name){
    console.log(`错误：缺少名字`)
    return
  }
  const path = process.argv[3] || ''
  await writeTemplate(name, path)
  
}

start()